package com.material.c2c.web.user.controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cpt.framework.web.JsonResult;
import com.material.c2c.user.entity.USER;
import com.material.c2c.user.service.UserService;
import com.material.c2c.web.user.model.LoginVO;
import com.material.c2c.web.user.model.QueryVO;
import com.material.c2c.web.util.ControllerUtil;

@RestController
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	@Autowired
	private HttpServletRequest request;
	@PostMapping("/search")
    public JsonResult search(@RequestBody QueryVO vo) {
      // 分页查询管理员信息
      return JsonResult.success(userService.search(vo.getKey(), vo.getPageable()));
    }
	@PostMapping("/add")
	public JsonResult add(@RequestBody USER user) {
		return ControllerUtil.getResult(userService.add(user));
	}
	@PostMapping("/update")
	public JsonResult update(@RequestBody USER user) {
		return ControllerUtil.getResult(userService.update(user));
	}
	@GetMapping("/delete/{id}")
	public JsonResult delete(@PathVariable String id) {
		return ControllerUtil.getResult(userService.delete(id));
	}
	@GetMapping("/getOne/{id}")
	public JsonResult getOne(@PathVariable String id) {
		return JsonResult.success(userService.getOne(id));
	}
	@PostMapping("/login")
	public JsonResult login(@RequestBody LoginVO vo,HttpServletResponse response) {
	  if(vo.getUserName() == null || "".equals(vo.getUserName())) {
	    return JsonResult.failed("输入账号有误！");
	  } 
	  if(vo.getPassword() == null || "".equals(vo.getPassword())) {
	    return JsonResult.failed("输入密码有误！");
	  }
	  USER user = userService.findUser(vo.getUserName());
	  if(user == null) {
	    return JsonResult.failed("账号不存在！");
	  }
	  if(!vo.getPassword().equals(user.getYhmm())) {
	    return JsonResult.failed("密码错误！");
	  }
	  HttpSession session = request.getSession();
	  session.setAttribute("user", user);
	  Cookie cookie = new Cookie("userId",user.getId());
	  response.addCookie(cookie);
	  userService.loginTiemPlus(user);
	  return JsonResult.success(user);
	}
	@GetMapping("/logout")
	public JsonResult logout(HttpServletResponse response) {
	  // 删除cookie
	  removeCookie(response,"userId");
	  // 删除session
	  HttpSession session = request.getSession();
	  session.removeAttribute("user");
	  return JsonResult.SUCCEED;
	}
	/**
	 * 移除cookie
	 * @param response
	 * @param name
	 * @since 1.0
	 * @version 1.0
	 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
	 */
	private void removeCookie(HttpServletResponse response,String name) {
	  Cookie[] cookies = request.getCookies();
      if (null==cookies) {
          return;
      } else {
          for(Cookie cookie : cookies){
              // 如果找到同名cookie，就将value设置为null，将存活时间设置为0，再替换掉原cookie，这样就相当于删除了。
              if(cookie.getName().equals(name)) {
                  cookie.setValue(null);
                  cookie.setMaxAge(0);
                  cookie.setPath("/");
                  response.addCookie(cookie);
                  break;
              }
          }
      }
	}
}
